package com.futonredemption.batterytest;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.os.PowerManager;

public class BatteryWasterService extends Service {

	public static final String EXTRA_TimeBetweenPings = "EXTRA_TimeBetweenPings";
	public static final String EXTRA_UseAlarmManager = "EXTRA_UseAlarmManager";
	public static final String EXTRA_UseWakeLock = "EXTRA_UseWakeLock";
	
	public static final String EXTRA_Action = "EXTRA_Action";
	public static final String EXTRA_Action_Start = "start";
	public static final String EXTRA_Action_Stop = "stop";
	
	@Override
	public void onStart(Intent intent, int startId) {
	    handleCommand(intent);
	}

	private void handleCommand(Intent intent) {

		// Default to the most abusive settings.
		int timeBetweenPings = 1;
		boolean useAlarmManager = false;
		boolean useWakeLocks = true;
		
		Thread.currentThread().setName("BatteryWasterService");
		
		
		
		if(intent == null) {
			// On service resurrection, sticky services accept null intents.
			// Persisted parameters as preferences because that's smart.
			
			final SharedPreferences sp = this.getSharedPreferences("NetworkBatteryTest", Context.MODE_PRIVATE);
			
			timeBetweenPings = sp.getInt(EXTRA_TimeBetweenPings, timeBetweenPings);
			useAlarmManager = sp.getBoolean(EXTRA_UseAlarmManager, useAlarmManager);
			useWakeLocks = sp.getBoolean(EXTRA_UseWakeLock, useWakeLocks);
			wasteTheBattery(timeBetweenPings, useAlarmManager, useWakeLocks);
		} else {
			final String action = intent.getStringExtra(EXTRA_Action);
			
			if(action.equals(EXTRA_Action_Stop)) {
				stopWastingBattery();
			} else {
				wasteTheBattery(timeBetweenPings, useAlarmManager, useWakeLocks);
			}
		}
	}

	private void stopWastingBattery() {
		this.stopSelf();
	}

	private boolean keepServiceAlive = true;
	
	private void wasteTheBattery(int timeBetweenPings, boolean useAlarmManager, boolean useWakeLocks) {
		// Persist the parameters as preferences.
		final SharedPreferences sp = this.getSharedPreferences("NetworkBatteryTest", Context.MODE_PRIVATE);
		final Editor prefEdit = sp.edit();
		
		prefEdit.putInt(EXTRA_TimeBetweenPings, timeBetweenPings);
		prefEdit.putBoolean(EXTRA_UseAlarmManager, useAlarmManager);
		prefEdit.putBoolean(EXTRA_UseWakeLock, useWakeLocks);
		prefEdit.commit();
		
		downloadSomeFile();
		
		if(useAlarmManager) {
			
		} else {
			
		}
		
		while(keepServiceAlive) {
			
			if(!useAlarmManager) {
				try {
					Thread.sleep(timeBetweenPings);
				} catch (InterruptedException e) {
					keepServiceAlive = false;
				}
			} else {
				this.keepServiceAlive = false;
			}
		}
	}

	private void downloadSomeFile() {
		// TODO: Change to be a 100KB file currently ~30KB
		FileDownloader.downloadFile("http://android-beryl.googlecode.com/hg/beryl-core/.pmd");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
	    handleCommand(intent);
	    // We want this service to continue running until it is explicitly
	    // stopped, so return sticky.
	    return START_STICKY;
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
